<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课程简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="detail">
              <j-editor v-model="model.detail" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品图片(2:1)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cover">
              <j-image-upload v-model="model.cover">
              </j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课程详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="images">
              <j-image-upload isMultiple v-model="model.images"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课程价钱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input-number v-model="model.price" placeholder="请输入课程价钱" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课程老师" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tid">
              <a-tree-select :treeDefaultExpandAll='true' :tree-data="sortLista" v-model="model.tid"
                :replaceFields="replaceFieldsa" :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }" allow-clear>
              </a-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课程类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isHot">
              <j-dict-select-tag type="list" v-model="model.isHot" dictCode="content_type" placeholder="请选择课程类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课程排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isHot">
              <a-input v-model="model.sort" placeholder="请输入课程排序"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MkContentBaseForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: "/course/add",
        edit: "/course/update",
        queryById: "/contentBase/mkContentBase/queryById"
      },
      sortLista: [],
      replaceFieldsa: {
        label: 'name',
        value: 'id'
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    handleTreeSelectChange(value) {
      // 处理选中值的变化
      console.log('选中的值:', value);
      this.secondaryid = value
      this.secondaryrequest()
    },
    shopChangea(shopId) {
      console.log(shopId)
      let that = this
      this.sortLista = []
      getAction('/teacher/page',).then(res => {
        if (res.success) {
          // res.result.forEach((e) => {
          //   e['selectable'] = false;
          // })
          that.sortLista = res.result.records
          console.log(this.sortLista, 11111)
        }
      })
    },
    add() {
      this.edit(this.modelDefault);
      this.shopChangea()
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
      this.shopChangea(record.shopId)
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'post';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>